<?php

namespace App\Admin\Controllers\Sys;

use App\Models\Sys\Ads;
use App\Models\Sys\AdsCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class AdsController extends Controller
{
    use ModelForm;
    public $header = '广告列表';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Ads::class, function (Grid $grid) {

            $grid->model()->with('category')->orderBy('add_id','desc');
            $grid->add_id('编号')->sortable();
            $grid->category()->name('分类');
            $grid->title('广告名称');
            $grid->image('广告图片')->image();
            $grid->column('时间')->display(function (){
                $start_time = $this->start_time ? '开始时间：' . date('Y-m-d H:i:s',$this->start_time) : '';
                $end_time = $this->end_time ? ('到期时间：' . date('Y-m-d H:i:s', $this->end_time)) : '';

                return $start_time . '<br>' . $end_time;
            });

            $grid->url('跳转链接');

            $grid->jump_type('跳转类型')->using([
                '1' => '不在内部跳转',
                '2' => '购买会员',
                '3' => '商品列表',
            ]);

            $states = [
                'on'  => ['value' => 1, 'text' => '上线', 'color' => 'success'],
                'off' => ['value' => 9, 'text' => '下线', 'color' => 'danger'],
            ];

            $grid->status('上线/下线')->switch($states);

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('title', '广告名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Ads::class, function (Form $form) {
            //$form->display('add_id', 'ID');
            $form->select('category_id', '广告位')->options(AdsCategory::options());
            $form->text('title', '广告名称');
            $form->number('ad_sort', '排序')->default(1);

            $form->datetime('start_time', '开始时间');
            $form->datetime('end_time', '到期时间');

            $states = [
                'on'  => ['value' => 1, 'text' => '上线', 'color' => 'success'],
                'off' => ['value' => 9, 'text' => '下线', 'color' => 'danger'],
            ];
            $form->switch('status','上线/下线')->states($states);

            $form->image('image', '广告图片');
            $form->url('url', '广告链接');
            $form->select('jump_type', '跳转类型')->options([
                '1' => '不在内部跳转',
                '2' => '购买会员',
                '3' => '商品列表',
            ]);

            //$form->file('url', '测试上传')->disk('admin');

            $form->disableViewCheck();
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->tools(function (Form\Tools $tools){
                $tools->disableView(); 
            });
        });
    }

}
